package com.snopy.list.linklist;

import java.util.Arrays;
import java.util.List;

/**
 * @author snopy
 * @description:
 * @classname NodeList
 * @date 2021/03/25 15:58
 * @email 77912204@qq.com
 */
public class Solution {

    public static ListNode ReverseList(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        while (cur!=null){
            //记录下一个节点
            ListNode next = cur.next;
            //将当前节点反转
            cur.next = prev;
            //移动新的头结点
            prev = cur;
            //移动当前节点
            cur = next;
        }
        return prev;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(5);
        ListNode listNode2 = new ListNode(4);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(2);
        ListNode listNode5 = new ListNode(1);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        System.out.println(toString(listNode1));
        listNode1 = ReverseList(listNode1);
        System.out.println(toString(listNode1));
    }
    public static String toString(ListNode head) {
        ListNode cur = head;
        String str = cur.val+"";
        while(cur.next!=null){
            cur = cur.next;
            str+=" "+cur.val;

        }
        return str;
    }
}
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }



}